package cn.fl.project.service.impl;

import cn.fl.commons.result.ResponseData;
import cn.fl.preloan.IPrjContractApiService;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.vo.PrjBscInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.dto.DataResultDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.api.IProjectModifyApiService;
import cn.fl.project.service.IPrjContService;

/**
 * 项目合同服务实现
 */
@Service
public class PrjContServiceImpl implements IPrjContService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(PrjContServiceImpl.class);

	@Autowired
	private IProjectModifyApiService projectModifyApiService;

	@Autowired
	private IPrjBscInfoService prjBscInfoService;

	@Autowired
	private IPrjContractApiService prjContractApiService;

	@Override
	public boolean modifyContPDFByPkIdAndOlContId(String pkId, String olContId) throws BaseException {
		String operName = CurrentThreadContext.getCurrentUserName();
		BaseLogUtils.info(logger, "modifyContPDFByPkIdAndOlContId",
				operName + "重新生成合同->pkId:" + pkId + " ->olContId:" + olContId, CurrentThreadContext.getCurrentUserName());
		Long crtUsrId = CurrentThreadContext.getCurrentUserId();
		// 判断，合同重构拆分，重新创建合同，将已切换到贷前生成的合同重新生成时也切换过去
		PrjBscInfoVO prjBscInfoVO = prjBscInfoService.selectById(Long.valueOf(pkId));
		if(prjBscInfoVO != null ){
			String insCode = prjBscInfoVO.getInsCode();
			String bugOrgCd = prjBscInfoVO.getBuOrgCd();
			if(PrjBscInfoConstant.PRJ_INS_CODE_SPD.equals(insCode) &&
					(PrjBscInfoConstant.CODE_BU_ORG_CD_ZK.equals(bugOrgCd) || PrjBscInfoConstant.CODE_BU_ORG_CD_ZC.equals(bugOrgCd) || PrjBscInfoConstant.CODE_BU_ORG_CD_XNY.equals(bugOrgCd))){
				ResponseData responseData = prjContractApiService.reCreate(Long.valueOf(olContId),false);
				if(responseData.isSuccess()){
					return true;
				}else {
					throw new BaseException(responseData.getMessage());
				}
			}
			// 上海农商 ，浦发本次不上线，方便合代码暂时不写在一起判断了
			if(PrjBscInfoConstant.PRJ_INS_CODE_SHNS.equals(insCode) || PrjBscInfoConstant.PRJ_INS_CODE_APFL.equals(insCode)
					|| PrjBscInfoConstant.PRJ_INS_CODE_ZBBANKX.equals(insCode)
                	|| PrjBscInfoConstant.PRJ_INS_CODE_YLBANK.equals(insCode)
					|| PrjBscInfoConstant.PRJ_INS_CODE_SSFL.equals(insCode)
					|| PrjBscInfoConstant.PRJ_INS_CODE_ICBCQD.equals(insCode)
					|| PrjBscInfoConstant.PRJ_INS_CODE_PABANK.equals(insCode)
					|| PrjBscInfoConstant.PRJ_INS_CODE_BOCBTBANK.equals(insCode)
			) {
				ResponseData responseData = prjContractApiService.reCreate(Long.valueOf(olContId),false);
				if(responseData.isSuccess()){
					return true;
				}else {
					throw new BaseException(responseData.getMessage());
				}
			}
		}
		return this.projectModifyApiService.modifyContPDFByPkIdAndOlContId(pkId, olContId, crtUsrId, null);
	}

	@Override
	public DataResultDTO chcekContAllIsSign(Long prjId, Long paySchmId, Long crtUsrId) throws BaseException {
		return this.projectModifyApiService.chcekContAllIsSign(prjId, paySchmId, crtUsrId);
	}
	
	@Override
	public boolean delContPDFByPkIdAndOlContId(String pkId, String olContId) throws BaseException {
		String operName = CurrentThreadContext.getCurrentUserName();
		BaseLogUtils.info(logger, "modifyContPDFByPkIdAndOlContId",
				operName + "作废合同->pkId:" + pkId + " ->olContId:" + olContId, CurrentThreadContext.getCurrentUserName());
		Long crtUsrId = CurrentThreadContext.getCurrentUserId();
		return this.projectModifyApiService.delContPDFByPkIdAndOlContId(pkId, olContId, crtUsrId);
	}

}
